using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/****************************************************
// 功能：伤害治疗
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/4/7 11:38:22
*****************************************************/

public class JumpNumWindow : MonoBehaviour
{
    
    private RectTransform rect;
    private Animator ani;
    private Text text;

    public int MaxFont;
    public int MinFont;
    public int MaxFontValue;
    public Color skillDamageColor;
    public Color buffDamageColor;
    public Color cureDamageColor;
    public Color slowSpeedColor;


    public JumpPool ownerPool;

    public void Init(JumpPool ownerPool)
    {
        this.ownerPool = ownerPool;
    }

    public void Show(JumpUpdateInfo jump)
    {
        int fontSize = (int)Mathf.Clamp(jump.jumpVal * 1.0f / MaxFontValue, MinFont, MaxFont);
        text.fontSize = fontSize;
        rect.anchoredPosition = jump.pos;

        switch(jump.jumpType) {
            case JumpTypeEnum.SkillDamage:
                text.text = jump.jumpVal.ToString();
                text.color = skillDamageColor;
                break;
            case JumpTypeEnum.BuffDamage:
                text.text = jump.jumpVal.ToString();
                text.color = buffDamageColor;
                break;
            case JumpTypeEnum.Cure:
                text.text = "+" + jump.jumpVal;
                text.color = cureDamageColor;
                break;
            case JumpTypeEnum.SlowSpeed:
                text.text = "减速";
                text.color = slowSpeedColor;
                break;
            case JumpTypeEnum.None:
            default:
                break;
        }

        switch(jump.jumpAni) {
            case JumpAniEnum.LeftCurve:
                ani.Play("JumpLeft", 0);
                break;
            case JumpAniEnum.RightCurve:
                ani.Play("JumpRight", 0);
                break;
            case JumpAniEnum.CenterUp:
                ani.Play("JumpCenter", 0);
                break;
            case JumpAniEnum.None:
            default:
                break;
        }

        StartCoroutine(Recycle());
    }
    
    private IEnumerator Recycle()
    {
        yield return new WaitForSeconds(0.75f);
        ani.Play("Empty");
        ownerPool.PushQueue(this);
    }

    private void Awake()
    {
        rect = transform.GetComponent<RectTransform>();
        ani = transform.Find("NumRoot").GetComponent<Animator>();
        text = transform.Find("NumRoot/Text").GetComponent<Text>();
    }
}

public class JumpPool
{
    private Transform poolRoot;
    private readonly Queue<JumpNumWindow> jumpNumQueue;

    public JumpPool(int count, Transform poolRoot)
    {
        this.poolRoot = poolRoot;
        jumpNumQueue = new Queue<JumpNumWindow>();

        for (var c = 0; c < count; c++)
        {
            PushQueue(CreateQueue());
        }
    }

    private JumpNumWindow CreateQueue()
    {
        var root = GameRoot.Instance.gameObject.transform.Find("WindowRoot/HpWindow(Clone)/JumpNumRoot").GetComponent<Transform>();
        var game = ResourceManager.Instance.LoadPrefab("Prefab/DynamicItem/JumpNum", root, true);
        //game.name = "JumpNum_" + Index;
        //game.transform.SetParent(poolRoot);
        game.transform.localPosition = Vector3.zero;
        game.transform.localScale = Vector3.one;

        var jump = game.GetComponent<JumpNumWindow>();
        jump.Init(this);

        return jump;
    }

    /// <summary>
    /// 取出队列
    /// </summary>
    /// <returns></returns>
    public JumpNumWindow PopQueue()
    {
        if (jumpNumQueue.Count > 0)
        {
            return jumpNumQueue.Dequeue();
        }
        else
        {
            Debug.LogWarning("飘字超额，动态调整上限");
            PushQueue(CreateQueue());
            return PopQueue();
        }
    }

    public void PushQueue(JumpNumWindow jump)
    {
        jumpNumQueue.Enqueue(jump);
    }
}

public enum JumpTypeEnum
{
    None,
    SkillDamage,
    BuffDamage,
    Cure,
    SlowSpeed
}

public enum JumpAniEnum
{
    None,
    LeftCurve,
    RightCurve,
    CenterUp
}

public class JumpUpdateInfo {
    public int jumpVal;
    public Vector2 pos;
    public JumpTypeEnum jumpType;
    public JumpAniEnum jumpAni;
}
